<?php

namespace app\admin\model\pifa;

use think\Model;
use think\Exception;
use think\Db;




class Goods extends Model
{

    

    

    // 表名
    protected $name = 'pifa_goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'goods_state_text',
        'open_switch_text'
    ];
    

     protected static function init()
    {
        

        self::beforeInsert(function ($row) {
         
            if ($row['goods_state'] == 1) {
                if (empty($row['goodsitem'])) {
                    throw new Exception('请添加对应的规格');
                }
            }

        });

        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
            $goodsitem = json_decode($row['goodsitem'],true);
            $pk = $row->getPk();
            foreach ($goodsitem as $key => $value) {
                $indata['goods_id'] = $row[$pk];
                $indata['name'] = $value['name'];
                $indata['price'] = $value['price'];
                $indata['kucun'] = $value['kucun'];
                Db::table('fa_pifa_goodsitem')->insert($indata);
            }
            return false;
        });
        self::afterWrite(function ($row) {
            $goodsitem = json_decode($row['goodsitem'],true);
            foreach ($goodsitem as $key => $value) {
               if (empty($value['id'])) {
                    $indata = [];
                    $indata['goods_id'] = $row['id'];
                    $indata['name'] = $value['name'];
                    $indata['price'] = $value['price'];
                    $indata['kucun'] = $value['kucun'];
                    Db::table('fa_pifa_goodsitem')->insert($indata);
               }else{
                    $map['goods_id'] = $row['id'];
                    $map['id'] = $value['id'];
                    $indata = [];
                    $indata['name'] = $value['name'];
                    $indata['price'] = $value['price'];
                    $indata['kucun'] = $value['kucun'];
                    Db::table('fa_pifa_goodsitem')->where($map)->update($indata);
               }
            }   
        });

        self::beforeWrite(function ($row) {
            $goodsitem = json_decode($row['goodsitem'],true);
            $ids = [];
            foreach ($goodsitem as $key => $value) {
               if (!empty($value['id'])) {
                  array_push($ids,$value['id']);
               }
            }   

            if (!empty($ids)) {
                $map['goods_id'] = $row['id'];
                $map['id'] = ['not in',$ids];
                Db::table('fa_pifa_goodsitem')->where($map)->delete();
            }          
        });
    }
    
    public function getGoodsStateList()
    {
        return ['0' => __('Goods_state 0'), '1' => __('Goods_state 1')];
    }

    public function getOpenSwitchList()
    {
        return ['0' => __('Open_switch 0'), '1' => __('Open_switch 1')];
    }


    public function getGoodsStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['goods_state']) ? $data['goods_state'] : '');
        $list = $this->getGoodsStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOpenSwitchTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['open_switch']) ? $data['open_switch'] : '');
        $list = $this->getOpenSwitchList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function fenlei()
    {
        return $this->belongsTo('Fenlei', 'fenlei_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
